﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Runtime.Serialization.Json;
using System.Text;
using System.IO;
using WindowsPhone7UXGuide;
using SharpGIS;
using Microsoft.Phone.Net.NetworkInformation;
using System.IO.IsolatedStorage;
using System.Windows.Navigation;
using Newtonsoft.Json;

namespace _4SQ
{
    public partial class PanoramaPage1 : PhoneApplicationPage
    {
        private IsolatedStorageSettings appSettings;

        string UserID = "15126026"; // Store/Use ID; My ID

      // string OAuthCode = "";
        string OAuthCode = "W3O44J0DALQC31JJYSN5SVRJQZ20X2GGNI2LEJDFWX0JC243"; // Store/Use OAuth Code for Requests

        // Ensure Data Connect.
        bool isConnected = NetworkInterface.GetIsNetworkAvailable();

        // Set if LoggedIn
        string bWeAreLoggedIn = "no";

        /* GoVenue Items
        string venueName = "";
        string venueAddress = "";
        string venueZipcode = "";
        string venueState = "";
        string venueCounty = "";
        string venueLat = "";
        string venueLon = "";
         */

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //Get OAuth Code
            if (this.NavigationContext.QueryString.ContainsKey("OAuthCode"))
            {
                OAuthCode = NavigationContext.QueryString["OAuthCode"];
            }
        }


        public PanoramaPage1()
        {
            InitializeComponent();
            appSettings = IsolatedStorageSettings.ApplicationSettings;
            GetFriends();
            GetCheckins();
        }
        private void GetFriends()
        {
            string ProviderURI = "https://api.foursquare.com/v2/users/"+UserID+"/friends?oauth_token="+OAuthCode+"&v=20120606";
          //  string ProviderURI = "https://api.foursquare.com/v2/users/self/friends?oauth_token=" + OAuthCode + "&v=20120606";
            GZipWebClient webClient = new GZipWebClient();
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            webClient.DownloadStringAsync(new Uri(ProviderURI));
        }

        void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                return;
            }
            using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(e.Result)))
            {
                DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(Friendies.RootObject));
                var providerResult = (Friendies.RootObject)dataContractJsonSerializer.ReadObject(stream);

                if (providerResult.response.friends.items == null)
                {
                    MessageBox.Show("It seems I can't obtain your Friends List... Please close this App and try again.", "Friends Error", MessageBoxButton.OK);
                    return;
                }

                if (providerResult.response.friends.items != null)
                {
                    TheFriend.ItemsSource = providerResult.response.friends.items;
                }
            }
        }

        private void GetCheckins()
        {
            string ProviderURI = "https://api.foursquare.com/v2/users/15126026/checkins?oauth_token=W3O44J0DALQC31JJYSN5SVRJQZ20X2GGNI2LEJDFWX0JC243&v=20120609";
            GZipWebClient webClient = new GZipWebClient();
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClientGetCheckins_DownloadStringCompleted);
            webClient.DownloadStringAsync(new Uri(ProviderURI));
        }

        void webClientGetCheckins_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                return;
            }

            JsonSerializer serializer = new JsonSerializer();
            PastCheckins.RootObject rootObject = (PastCheckins.RootObject)serializer.Deserialize(new JsonTextReader(new StringReader(e.Result)), typeof(PastCheckins.RootObject));

            foreach (var tweet in rootObject.response.checkins.items)
            {
                MessageBox.Show(tweet.venue.name);
            }

           using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(e.Result)))
            {
                DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(PastCheckins.RootObject));
                var providerResult = (PastCheckins.RootObject)dataContractJsonSerializer.ReadObject(stream);

                if (providerResult == null)
                {
                    //error
                }

                if (providerResult != null)
                {
                    oldCheckins.ItemsSource = providerResult.response.checkins.items;
                }
            }
        }

     /*   private void VenueName_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string choosen;
            TextBlock butt = sender as TextBlock;
            choosen = butt.Tag.ToString();


            //Send to Venue Page
            NavigationService.Navigate(new Uri("/Venue.xaml?VenueID=" + butt.Tag.ToString(), UriKind.Relative));
    
        } */
    }
}